package com.googlecode.restitory.gae.rhino;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashUtil {

	public static String md5(byte[] bytes) {
		try {
			MessageDigest digester = MessageDigest.getInstance("MD5");
			byte[] hash = digester.digest(bytes);
			StringBuilder sb = new StringBuilder();
			for (byte b : hash) {
				String h = Integer.toString(0xFF & b, 16);
				if (h.length() == 1) {
					sb.append('0');
				}
				if (h.length() > 2) {
					throw new RuntimeException("error");
				}
				sb.append(h);
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}

}
